package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.insurance;

import com.bmps.springcloud.common.utils.DateUtil;
import com.bmps.springcloud.insuredmicroservice.config.accept.ProductConfig;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.UnderWriteConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.*;
import com.bmps.springcloud.insuredmicroservice.model.underwrite.InsuredModel;
import com.bmps.springcloud.insuredmicroservice.repository.underwrite.coresystem.InsuredCoreRepository;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author lyw
 * @date 2019-07-30 10:51:39
 */
public class Hdt051InsuranceStrategy implements InsuranceStrategy {
    @Override
    public InsuredModel checkInsuredRolesByRiskCode(AcceptRequestDataModel acceptRequestDataModel, InsuredCoreRepository insuredCoreRepository,
                                                    ProductModel productModel, ProductConfig productConfig) {
        RequestModel requestModel = acceptRequestDataModel.getRequest();
        CustInfoModel custInfoModel = requestModel.getCustInfo();
        //投保人信息
        AppntModel appntModel = custInfoModel.getAppnt();
        //2.1职业类别
        String type = appntModel.getOccupationType();
        if (StringUtils.isEmpty(type)) {
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE, UnderWriteConstant.UNDER_WRITE_NUGG0026_MESSAGE);
        }
        if (!(productConfig.getAppntTypeList().contains(type))) {
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE, String.format(UnderWriteConstant.UNDER_WRITE_NPGG0028_MESSAGE,type));
        }
        Date cValiDate = requestModel.getPolicyInfo().getCvalidate();
        Date birthday = appntModel.getAppntBirthday();
        int days = DateUtil.getDays(birthday, cValiDate);
        int years = DateUtil.getYears(birthday, cValiDate);
        //校验投保人年龄
        if (!(days >= UnderWriteConstant.AGE_THIRTY_DAY && years <= UnderWriteConstant.AGE_FIFTY_FIVE_YEAR)) {
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                    String.format(UnderWriteConstant.UNDER_WRITE_NUGG0030_MESSAGE,appntModel.getAppntName()));
        }
        //投保人黑名单
        if (appntModel.getBlackListFlag() > 0) {
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                    String.format(UnderWriteConstant.UNDER_WRITE_NUGG0032_MESSAGE, appntModel.getAppntName()));
        }
        //投保人理赔
        if(appntModel.getClaimFlag()>0){
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                    String.format(UnderWriteConstant.UNDER_WRITE_NUGG0033_MESSAGE, appntModel.getAppntName()));
        }
        //被保人信息
        List<InsurantModel> insurantModelList = custInfoModel.getInsurants().getInsurant();
        for (InsurantModel insurantModel : insurantModelList) {
            int insSeq = insurantModel.getInsSeq();
            if (insSeq == productModel.getProdInsSeq()) {
                type = insurantModel.getOccupationType();
                if (StringUtils.isEmpty(type)) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE, UnderWriteConstant.UNDER_WRITE_NPGG0017_MESSAGE);
                }
                //3.非HCT011的险种，职业类别不能为5、6、7类
                if (productConfig.getInsurantTypeList().contains(type)) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                            String.format(UnderWriteConstant.UNDER_WRITE_NPGG0016_MESSAGE,type));
                }
                //1-主被保人，2-第二被保人,3-第三被保人，4-第四被保人，5-第五被保人
                if (insSeq == 1) {
                    if (StringUtils.equals(appntModel.getAppntName(), insurantModel.getName())
                            && StringUtils.equals(appntModel.getAppntSex(), insurantModel.getSex())
                            && StringUtils.equals(appntModel.getIdNo(), insurantModel.getIdNo())
                            && StringUtils.equals(appntModel.getIdType(), insurantModel.getIdType())
                            && StringUtils.equals(DateUtil.format(appntModel.getAppntBirthday(), "yyyy-MM-dd"), DateUtil.format(insurantModel.getBirthday(), "yyyy-MM-dd")))
                    {
                        return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,UnderWriteConstant.UNDER_WRITE_NUGG0024_MESSAGE);
                    }
                }
                if (insurantModel.getBlackListFlag() > 0) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_NUGG0031,
                            String.format(UnderWriteConstant.UNDER_WRITE_NUGG0031_MESSAGE,insurantModel.getName()));
                }
                if (insurantModel.getClaimFlag() > 0) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_NUGG0027,
                            String.format(UnderWriteConstant.UNDER_WRITE_NUGG0027_MESSAGE,insurantModel.getName()));
                }
            }
        }
        return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_SUCCESS_CODE, AcceptConstant.RESULT_SUCCESS_MESSAGE);

    }
}
